package com.example.doctor.controller.Center;

import com.alibaba.fastjson.JSONObject;
import com.example.common.cqsupervision.dto.req.CaVerifyReq;
import com.example.common.entity.DoctorBaseInfo;
import com.example.common.support.Result;
import com.example.common.utils.RequestHelp;
import com.example.doctor.dto.req.ConfigReq;
import com.example.doctor.dto.req.PcRxListReq;
import com.example.doctor.service.DoctorCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "个人中心")
@RequestMapping("/api/Center/Doctor")
public class DoctorController {

    @Resource
    DoctorCenterService doctorCenterService;

    //#region 个人信息

    @ApiOperation(value = "个人中心")
    @PostMapping("/MyIndex")
    public Result<Map<String, Object>> MyIndex(@RequestHeader Long UID) {
        return Result.success(doctorCenterService.MyIndex(UID));
    }

    @ApiOperation(value = "个人信息")
    @PostMapping("/MyInfo")
    public Result<Map<String, Object>> MyInfo(@RequestHeader Long UID) {
        return Result.success(doctorCenterService.MyInfo(UID));
    }

    @ApiOperation(value = "个人信息 保存")
    @PostMapping("/MyInfoSave")
    public Result<Boolean> MyInfoSave(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String CertificateCode = Jo.getString("CertificateCode");
        String Intro = Jo.getString("Intro");

        DoctorBaseInfo req = new DoctorBaseInfo();
        req.setDcId(UID);
        req.setCertificateCode(CertificateCode);
        req.setIntro(Intro);

        Boolean sess = doctorCenterService.MyInfoSave(req);
        return Result.success(sess);
    }

    @ApiOperation(value = "上传头像")
    @PostMapping(value = "/UpLoadHead")
    public Result<Object> UpLoadHead(@RequestParam(value = "file") MultipartFile file) {
        return Result.success(doctorCenterService.ImgUpLoad(file));
    }

    @ApiOperation(value = "保存头像")
    @PostMapping("/MyHeadSave")
    public Result<Object> MyHeadSave(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String Head = Jo.getString("Head");

        DoctorBaseInfo req = new DoctorBaseInfo();
        req.setDcId(UID);
        req.setHead(Head);

        Boolean sess = doctorCenterService.MyHeadSave(req);
        return Result.success(sess);
    }
    //#endregion

    //#region 问诊订单
    @ApiOperation(value = "问诊订单")
    @PostMapping("/IMOrderList")
    public Result<List<Map<String, Object>>> IMOrderList(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        int AType = Jo.getIntValue("AType");
        int page = Jo.getIntValue("page");
        int limit = Jo.getIntValue("limit");
        return Result.success(doctorCenterService.IMOrderList(AType, page, limit, UID));
    }
    //#endregion

    //#region 配置服务
    @ApiOperation(value = "配置服务统计")
    @PostMapping("/ServiceCount")
    public Result<List<Map<String, Object>>> ServiceCount(@RequestHeader Long UID) {
        return Result.success(doctorCenterService.DocATypeCount(UID));
    }

    @ApiOperation(value = "配置服务统计")
    @PostMapping("/ServiceInfo")
    public Result<Map<String, Object>> ServiceInfo(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        int AType = Jo.getIntValue("AType");
        return Result.success(doctorCenterService.ServiceInfo(UID, AType));
    }

    @ApiOperation(value = "开关服务")
    @PostMapping("/EditService")
    public Result<Boolean> EditService(@RequestHeader Long UID, @RequestBody ConfigReq req) {
        return Result.success(doctorCenterService.EditService(UID, req));
    }

    @ApiOperation(value = "开处方类型")
    @PostMapping("/GetPrescription")
    public Result<Map<String, Object>> GetPrescription(@RequestHeader Long UID) {
        return Result.success(doctorCenterService.GetPrescription(UID));
    }

    @ApiOperation(value = "开处方类型")
    @PostMapping("/EditPrescription")
    public Result<Boolean> EditPrescription(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        boolean ZPrescription = Jo.getBoolean("ZPrescription");
        boolean XPrescription = Jo.getBoolean("XPrescription");
        return Result.success(doctorCenterService.EditPrescription(UID, ZPrescription, XPrescription));
    }
    //#endregion

    //#region 处方列表
    @ApiOperation(value = "中药用药建议记录")
    @PostMapping("/ZRxList")
    public Result<List<Map<String, Object>>> ZRxList(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        int page = Jo.getIntValue("page");
        int limit = Jo.getIntValue("limit");
        return Result.success(doctorCenterService.ZRxList(page, limit, UID));
    }

    @ApiOperation(value = "西药处方记录")
    @PostMapping("/XRxList")
    public Result<List<Map<String, Object>>> XRxList(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        int page = Jo.getIntValue("page");
        int limit = Jo.getIntValue("limit");
        return Result.success(doctorCenterService.XRxList(page, limit, UID));
    }

    //#endregion

    //#region 评价
    @ApiOperation(value = "评论列表", notes = "{\"page\":1,\"limit\":10,\"IsComment\":\"\",\"search\":\"\"}")
    @PostMapping("/CommentList")
    public Result<Map<String, Object>> CommentList(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        int page = Jo.getIntValue("page");
        int limit = Jo.getIntValue("limit");
        String search = Jo.getString("search");
        String IsComment = Jo.getString("IsComment");
        return Result.success(doctorCenterService.CommentList(page, limit, UID, IsComment, search));
    }

    @ApiOperation(value = "评论详情")
    @PostMapping("/CommentDetail")
    public Result<Map<String, Object>> CommentDetail(@RequestHeader Long UID, HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");
        return Result.success(doctorCenterService.CommentDetail(UID, OrderNO));
    }

    @ApiOperation(value = "回复评论")
    @PostMapping("/ReComment")
    public Result<Boolean> ReComment(HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");
        String Re = Jo.getString("Re");
        return Result.success(doctorCenterService.ReComment(OrderNO, Re));
    }
    //#endregion

    //#region PC
    @ApiOperation(value = "PC处方记录")
    @PostMapping("/PcRxList")
    public Result<Map<String, Object>> PcRxList(@RequestBody PcRxListReq req) {

        return Result.success(doctorCenterService.PcRxList(req));
    }

    /**
     * 获得当前用户的激活码
     *
     * @param phone 用户的手机号
     * @return 返回用户激活信息
     */
    @GetMapping("/getCaMessage")
    @ApiOperation(value = "获得当前用户的激活码")
    public Result<Map<String, String>> getCaMessage(String phone) {
        return Result.success(doctorCenterService.getCaMessage(phone));
    }


    /**
     * 提交签名任务
     *
     * @param orderId 订单id
     * @return 返回任务id
     */
    @GetMapping("/addSignJob")
    @ApiOperation("获得签名任务id")
    public Result<Map<String, String>> addSignJob(@ApiParam(required = true, name = "orderId", value = "订单id")
                                                          String orderId,
                                                  @ApiParam(required = true, name = "msspId", value = "用户编号")
                                                          String msspId) {
        return Result.success(doctorCenterService.addSignJob(orderId, msspId));
    }

    /**
     * 获得签名结果
     *
     * @param signJobId 任务id
     * @param msspId    用户编号
     * @return 返回相关x响应参数
     */
    @GetMapping("/querySignJob")
    @ApiOperation("获得签名结果")
    public Result<Map<String, String>> querySignJob(@ApiParam(required = true, name = "signJobId", value = "任务id")
                                                            String signJobId,
                                                    @ApiParam(required = true, name = "msspId", value = "用户编号")
                                                            String msspId,
                                                    @ApiParam(required = true, name = "orderId", value = "订单id")
                                                            String orderId) {
        return Result.success(doctorCenterService.querySignJob(signJobId, msspId, orderId));
    }

    /**
     * 获取是否签名成功
     *
     * @param caVerifyReq 请求封装类
     * @return 是否签名成功
     */
    @PostMapping("/verifySign")
    @ApiOperation("校验签名是否成功")
    public Result<Map<String, String>> verifySign(@ApiParam(required = true, value = "caVerifyReq", name = "签名结果请求类")
                                                  @RequestBody CaVerifyReq caVerifyReq) {

        return Result.success(doctorCenterService.verifySign(caVerifyReq.getPlain(), caVerifyReq.getSignResult(), caVerifyReq.getCert(), caVerifyReq.getOrderId(), caVerifyReq.getTimeSignature()));
    }


    //#endregion

}
